#define _CRT_SECURE_NO_WARNINGS 1

#include "queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->front = q->rear = NULL;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	newnode->data = data;
	newnode->next = NULL;
	if (QueueEmpty(q))
	{
		q->front = newnode;
	}
	else
	{
		q->rear->next = newnode;
	}
	q->rear = newnode;
}

void QueuePop(Queue* q)
{
	assert(q);
	if (QueueSize(q) == 1)
	{
		free(q->front);
		q->front = q->rear = NULL;
	}
	else
	{
		QNode* next = q->front->next;
		free(q->front);
		q->front = next;
	}
}


int QueueSize(Queue* q)
{
	assert(q);
	QNode* cur = q->front;
	int size = 0;
	while (cur)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

int QueueEmpty(Queue* q)
{
	assert(q);
	return q->front == NULL;
}


QDataType QueueFront(Queue* q)
{
	assert(q);
	if (!QueueEmpty(q))
	{
		return q->front->data;
	}
	else
	{
		return 0;
	}
}

QDataType QueueBack(Queue* q)
{
	assert(q);
	if (!QueueEmpty(q))
	{
		return q->rear->data;
	}
	else
	{
		return 0;
	}
}

void QueueDestroy(Queue* q)
{
	assert(1);
	while (!QueueEmpty(q))
	{
		QNode* next = q->front->next;
		free(q->front);
		q->front = next;
	}
	q->front = q->rear = NULL;
}


void QueuePrint(Queue* q)
{
	assert(q);
	if (!QueueEmpty(q))
	{
		QNode* cur = q->front;
		while (cur)
		{
			printf("%d ", cur->data);
			cur = cur->next;
		}
		printf("\n");
	}
}
